#!/bin/bash -e

# Copyright 2021 Flant JSC
# Licensed under the Deckhouse Platform Enterprise Edition (EE) license. See https://github.com/deckhouse/deckhouse/blob/main/ee/LICENSE


for f in $(find /frameworks/shell/ -type f -iname "*.sh"); do
  source $f
done

function __config__() {
  if [ "$INLET" == "LoadBalancer" ]; then
    cat << EOF
      configVersion: v1
      kubernetes:
      - name: ingressgateway_service
        apiVersion: v1
        kind: Service
        group: main-lb
        keepFullObjectsInMemory: false
        namespace:
          nameSelector:
            matchNames: [d8-istio]
        nameSelector:
          matchNames: [ingressgateway]
        jqFilter: |
          {
            "address": (.status.loadBalancer.ingress[0].ip // .status.loadBalancer.ingress[0].hostname // null),
            "port": (.spec.ports[] | select(.name == "tls") | .port)
          }
EOF
  elif [ "$INLET" == "NodePort" ]; then
    cat << EOF
      configVersion: v1
      kubernetes:
      - name: ingressgateway_service
        apiVersion: v1
        kind: Service
        group: main-np
        keepFullObjectsInMemory: false
        namespace:
          nameSelector:
            matchNames: [d8-istio]
        nameSelector:
          matchNames: [ingressgateway]
        jqFilter: |
          {
            "port": (.spec.ports[] | select(.name == "tls") | .nodePort)
          }
      - name: pods
        apiVersion: v1
        kind: Pod
        group: main-np
        keepFullObjectsInMemory: false
        namespace:
          nameSelector:
            matchNames: [d8-istio]
        labelSelector:
          matchLabels:
            app: ingressgateway
        jqFilter: |
          {
            "nodeName": .spec.nodeName
          }
      - name: nodes
        apiVersion: v1
        kind: Node
        group: main-np
        keepFullObjectsInMemory: false
        jqFilter: |
          {
            "name": .metadata.name,
            "address": ((.status.addresses[] | select(.type == "ExternalIP") | .address) // (.status.addresses[] | select(.type == "InternalIP") | .address)),
            "isActive": (
              all((.spec.taints // [])[] ; .key != "node.kubernetes.io/unschedulable")
              and
              ((.status.conditions // [])[] | select(.type == "Ready") | .status == "True")
            )
          }
EOF
  fi
}

function __on_group::main-lb() {
  context::jq '
    [
      .snapshots.ingressgateway_service[].filterResult | select(.address and .port) | {"address": .address, "port": .port}
    ]' > /metadata/ingressgateways-array.json

}

function __on_group::main-np() {
  context::jq '
    .snapshots.ingressgateway_service[0].filterResult.port as $port |
    [.snapshots.pods[].filterResult.nodeName] as $pod_nodes |
    [
        .snapshots.nodes[].filterResult | select(.isActive) | .name as $name | select($pod_nodes | index($name)) | {"address": .address, "port": $port}
    ]' > /metadata/ingressgateways-array.json
}

hook::run "$@"
